//
//  UIButton+AdjustTextAnsImage.m
//  homework
//
//  Created by gaofeng on 15/2/28.
//  Copyright (c) 2015年 Baidu. All rights reserved.
//

#import "UIButton+AdjustTextAnsImage.h"

@implementation UIButton(AdjustTextAnsImage)

- (void)setImageAndTextToCenterWithSpaceDep:(CGFloat)space {
    UILabel *label  = self.titleLabel;
    UIImageView *imageView = self.imageView;
    
    if (label.text.length <= 0) {
        return;
    }
    
    if (imageView.image == nil) {
        return;
    }
    
    CGFloat labelHeight = label.frame.size.height;
    [label sizeToFit];
    CGFloat labelWidth = label.frame.size.width;
    CGFloat imageWidth = imageView.frame.size.width;
    
    if (labelWidth + imageWidth + space > self.bounds.size.width) {
        space = self.bounds.size.width - labelWidth - imageWidth;
        
        if (space <= 0) {
            labelWidth += space;
            space = 0;
        }
    }
    
    CGFloat orginX = self.bounds.size.width - labelWidth - imageWidth - space;
    
    orginX = (NSInteger)(orginX / 2);
    
    CGRect frame = label.frame;
    frame.origin.x = orginX;
    frame.size.width = labelWidth;
    frame.size.height = labelHeight;
    self.titleLabel.frame = frame;
    
    orginX += labelWidth + space;
    
    frame = imageView.frame;
    frame.origin.x = orginX;
    imageView.frame = frame;
}


- (void)setImageAndTextToCenterWithSpace:(CGFloat)space {
    UILabel *label  = self.titleLabel;
    UIImageView *imageView = self.imageView;
    
    if (label.text.length <= 0) {
        return;
    }
    
    if (imageView.image == nil) {
        return;
    }
    
    CGRect frame = label.frame;
    [label sizeToFit];
    CGFloat labelWidth = label.frame.size.width;
    CGFloat imageWidth = imageView.frame.size.width;
    label.frame = frame;
    
    if (labelWidth + imageWidth + space > self.bounds.size.width) {
        space = self.bounds.size.width - labelWidth - imageWidth;
        
        if (space <= 0) {
            labelWidth += space;
            space = 0;
        }
    }
    
    labelWidth += 10;
    CGFloat orginX = self.bounds.size.width - labelWidth - imageWidth - space;
    
    orginX = (NSInteger)(orginX / 2);
    
    UIEdgeInsets inset = UIEdgeInsetsMake(0, orginX, 0, self.bounds.size.width - labelWidth-orginX-space);
    self.titleEdgeInsets = inset;
    
    orginX += labelWidth + space;
    inset = UIEdgeInsetsMake(0, orginX, 0, self.bounds.size.width - orginX - imageWidth);
    self.imageEdgeInsets = inset;
}

- (void)setImageAndTextToUpDownWithSpace:(CGFloat)space {
    
    UILabel *label  = self.titleLabel;
    UIImageView *imageView = self.imageView;
    
    CGSize labelSize = [label.text sizeWithAttributes:@{NSFontAttributeName:label.font}];
    
    if (label.text.length <= 0) {
        return;
    }
    
    if (imageView.image == nil) {
        return;
    }
    
    CGFloat labelHeight = labelSize.height;
    CGFloat imageHeight = imageView.frame.size.height;
    CGFloat imageWidth = imageView.frame.size.width;
    
    if (labelHeight + imageHeight + space > self.bounds.size.height) {
        space = self.bounds.size.height - labelHeight - imageHeight;
        
        if (space <= 0) {
            imageHeight += space;
            space = 0;
        }
    }
    
    CGFloat orginY = self.bounds.size.height - labelHeight - imageHeight - space;
    orginY = (NSInteger)(orginY / 2);
    CGFloat totalHeight = (imageHeight + labelHeight + space);
    
    label.frame = CGRectMake(ceilf(label.frame.origin.x), ceilf(label.frame.origin.y), label.frame.size.width, label.frame.size.height);
    imageView.frame = CGRectMake(ceilf(imageView.frame.origin.x), ceilf(imageView.frame.origin.y), imageView.frame.size.width, imageView.frame.size.height);
    self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageHeight), 0.0, 0.0, - labelSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageWidth, - (totalHeight - labelHeight),0.0);

}


- (void)layoutButtonWithEdgeInsetsStyle:(ButtonEdgeInsetsStyle)style imageTitlespace:(CGFloat)space {
    CGFloat imageViewWidth = CGRectGetWidth(self.imageView.frame);
    CGSize titleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    CGFloat labelWidth = titleSize.width;
    
    CGFloat imageInsetsTop = 0.0f;
    CGFloat imageInsetsLeft = 0.0f;
    CGFloat imageInsetsBottom = 0.0f;
    CGFloat imageInsetsRight = 0.0f;
    
    CGFloat titleInsetsTop = 0.0f;
    CGFloat titleInsetsLeft = 0.0f;
    CGFloat titleInsetsBottom = 0.0f;
    CGFloat titleInsetsRight = 0.0f;
    
    switch (style) {
        case ButtonEdgeInsetsStyleImageRight:
        {
            space = space * 0.5;
            
            imageInsetsLeft = labelWidth + space;
            imageInsetsRight = -imageInsetsLeft;
            
            titleInsetsLeft = - (imageViewWidth + space);
            titleInsetsRight = -titleInsetsLeft;
            
            
            if (labelWidth+imageViewWidth+space > CGRectGetWidth(self.frame)-6) {
                titleInsetsLeft += 5;
                titleInsetsRight += 5;
                imageInsetsLeft = labelWidth - 10 + space;
                imageInsetsRight = -imageInsetsLeft;
            }
            
        }
            break;
            
        case ButtonEdgeInsetsStyleImageLeft:
        {
            space = space * 0.5;
            
            imageInsetsLeft = -space;
            imageInsetsRight = -imageInsetsLeft;
            
            titleInsetsLeft = space;
            titleInsetsRight = -titleInsetsLeft;
        }
            break;
        case ButtonEdgeInsetsStyleImageBottom:
        {
            CGFloat imageHeight = CGRectGetHeight(self.imageView.frame);
            CGFloat labelHeight = CGRectGetHeight(self.titleLabel.frame);
            CGFloat buttonHeight = CGRectGetHeight(self.frame);
            CGFloat boundsCentery = (imageHeight + space + labelHeight) * 0.5;
            
            CGFloat centerX_button = CGRectGetMidX(self.bounds); // bounds
            CGFloat centerX_titleLabel = CGRectGetMidX(self.titleLabel.frame);
            CGFloat centerX_image = CGRectGetMidX(self.imageView.frame);
            
            CGFloat imageBottomY = CGRectGetMaxY(self.imageView.frame);
            CGFloat titleTopY = CGRectGetMinY(self.titleLabel.frame);
            
            imageInsetsTop = buttonHeight - (buttonHeight * 0.5 - boundsCentery) - imageBottomY;
            imageInsetsLeft = centerX_button - centerX_image;
            imageInsetsRight = - imageInsetsLeft;
            imageInsetsBottom = - imageInsetsTop;
            
            titleInsetsTop = (buttonHeight * 0.5 - boundsCentery) - titleTopY;
            titleInsetsLeft = -(centerX_titleLabel - centerX_button);
            titleInsetsRight = - titleInsetsLeft;
            titleInsetsBottom = - titleInsetsTop;
            
        }
            break;
        case ButtonEdgeInsetsStyleImageTop:
        {
            CGFloat imageHeight = CGRectGetHeight(self.imageView.frame);
            CGFloat labelHeight = CGRectGetHeight(self.titleLabel.frame);
            CGFloat buttonHeight = CGRectGetHeight(self.frame);
            CGFloat boundsCentery = (imageHeight + space + labelHeight) * 0.5;
            
            CGFloat centerX_button = CGRectGetMidX(self.bounds); // bounds
            CGFloat centerX_titleLabel = CGRectGetMidX(self.titleLabel.frame);
            CGFloat centerX_image = CGRectGetMidX(self.imageView.frame);
            
            CGFloat imageTopY = CGRectGetMinY(self.imageView.frame);
            CGFloat titleBottom = CGRectGetMaxY(self.titleLabel.frame);
            
            imageInsetsTop = (buttonHeight * 0.5 - boundsCentery) - imageTopY;
            imageInsetsLeft = centerX_button - centerX_image;
            imageInsetsRight = - imageInsetsLeft;
            imageInsetsBottom = - imageInsetsTop;
            
            titleInsetsTop = buttonHeight - (buttonHeight * 0.5 - boundsCentery) - titleBottom;
            titleInsetsLeft = -(centerX_titleLabel - centerX_button);
            titleInsetsRight = - titleInsetsLeft;
            titleInsetsBottom = - titleInsetsTop;
        }
            break;
            
        default:
            break;
    }
    
    self.titleLabel.frame = CGRectMake(ceilf(self.titleLabel.frame.origin.x), ceilf(self.titleLabel.frame.origin.y), self.titleLabel.frame.size.width, self.titleLabel.frame.size.height);
    self.imageView.frame = CGRectMake(ceilf(self.imageView.frame.origin.x), ceilf(self.imageView.frame.origin.y), self.imageView.frame.size.width, self.imageView.frame.size.height);
    self.imageEdgeInsets = UIEdgeInsetsMake(imageInsetsTop, imageInsetsLeft, imageInsetsBottom, imageInsetsRight);
    self.titleEdgeInsets = UIEdgeInsetsMake(titleInsetsTop, titleInsetsLeft, titleInsetsBottom, titleInsetsRight);
}


@end
